-- 当超过编辑器的边界视觉上换行了，其实还是一行时，跳行的以实际算一行计算
-- https://www.reddit.com/r/vim/comments/2k4cbr/problem_with_gj_and_gk/
vim.keymap.set(
  "n",
  "j",
  [[v:count ? 'j' : 'gj']],
  { noremap = true, expr = true }
)
vim.keymap.set(
  "n",
  "k",
  [[v:count ? 'k' : 'gk']],
  { noremap = true, expr = true }
)

-- vim 的原生文档树, 使用 nvim-tree 替代 netrwPlugin
-- vim.keymap.set('', '<leader>e', '<Cmd>Lex 26<CR>', { noremap = true, silent = true })

-- Use jk to exit insert mode
vim.keymap.set(
  "i",
  "jk",
  "<Esc>",
  { desc = "Exit insert mode with jk", noremap = true, silent = true }
)

-- File Buffer Save
-- vim.keymap.set("n", "<leader>w", "<Esc><Cmd>write<CR>", { noremap = true, silent = true, desc = "Buffer save" })
vim.keymap.set(
  { "i", "n" },
  "<C-s>",
  "<Esc><Cmd>write<CR>",
  { noremap = true, silent = true }
)

-- Close current
-- vim.keymap.set("n", "<leader>sc", "<C-w>q")
-- Close other
-- vim.keymap.set("n", "<leader>so", "<C-w>o")
-- Close all
-- vim.keymap.set({ "n", "v" }, "<Space>sa", "<C-w>o<Cmd>qa<CR>")
-- Close all
-- vim.keymap.set(
--   { "n", "v" },
--   "<Space>sa",
--   "<Cmd>qall<CR>",
--   { desc = "Close all buffers", noremap = true, silent = true }
-- )
-- Close current
vim.keymap.set(
  { "n", "v" },
  "<S-q>",
  "<Cmd>quit<CR>",
  { desc = "Close current buffer", noremap = true, silent = true }
)

-- define common options
local opts = {
  noremap = true, -- non-recursive
  silent = true,  -- do not show message
}

-- delete single character without copying into register
-- vim.keymap.set("n", "x", '"_x')

-- increment/decrement numbers
vim.keymap.set("n", "<leader>+", "<C-a>", { desc = "Increment number" }) -- increment
vim.keymap.set("n", "<leader>-", "<C-x>", { desc = "Decrement number" }) -- decrement

-- Select all
vim.keymap.set("n", "<C-a>", "gg<S-v>G", opts)

-- clear search highlights
vim.keymap.set(
  "n",
  "<leader>nh",
  "<Cmd>nohl<CR>",
  { silent = true, noremap = true, desc = "Clear search highlights" }
)

-- 撤销, 默认是 u
vim.keymap.set({ "n", "i" }, "<C-z>", "<Cmd>undo<CR>", opts)
-- Ctrl-r  恢复撤销的操作

-- window management
vim.keymap.set(
  "n",
  "<leader>sv",
  "<C-w>v",
  { desc = "Split window vertically" }
) -- split window vertically
vim.keymap.set(
  "n",
  "<leader>sh",
  "<C-w>s",
  { desc = "Split window horizontally" }
)                                                                                -- split window horizontally
vim.keymap.set("n", "<leader>se", "<C-w>=", { desc = "Make splits equal size" }) -- make split windows equal width & height
vim.keymap.set(
  "n",
  "<leader>sx",
  "<cmd>close<CR>",
  { desc = "Close current split" }
)                                                                               -- close current split window

vim.keymap.set("n", "<leader>to", "<cmd>tabnew<CR>", { desc = "Open new tab" }) -- open new tab
vim.keymap.set(
  "n",
  "<leader>tx",
  "<cmd>tabclose<CR>",
  { desc = "Close current tab" }
)                                                                               -- close current tab
vim.keymap.set("n", "<leader>tn", "<cmd>tabn<CR>", { desc = "Go to next tab" }) --  go to next tab
vim.keymap.set(
  "n",
  "<leader>tp",
  "<cmd>tabp<CR>",
  { desc = "Go to previous tab" }
) --  go to previous tab
vim.keymap.set(
  "n",
  "<leader>tf",
  "<cmd>tabnew %<CR>",
  { desc = "Open current buffer in new tab" }
) --  move current buffer to new tab

-- Shift + a 在行尾插入, Shift + i 在行首插入

-- ct字符  dt字符  ->  删除字符前的所有字符
-- cf字符  df字符  ->  删除字符前的所有字符,包含该字符

-- 使用 <Shift-u> 将光标所在单词转化为全大写
vim.keymap.set({ "i", "v", "n" }, "<S-A-u>", "vawU", { desc = "Uppercase" }) -- 光标跳到本词开始
-- vim.keymap.set({ "i", "v", "n" }, "<S-C-u>", "<ESC>viwUw") -- 光标跳到本词末尾
-- 使用 <Shift-l> 将光标所在单词转化为全小写
vim.keymap.set({ "i", "v", "n" }, "<S-A-l>", "vawu", { desc = "Lowercase" })
-- vim.keymap.set({ "i", "v", "n" }, "<S-C-l>", "<ESC>viwuw")

-- Configure Copy Shortcuts
vim.keymap.set({ "v", "n", "i" }, "<C-c>", '"+y') -- copy
-- vim.keymap.set("i", "<LeftRelease>", '"+ygv') -- Paste with middle mouse click
-- vim.keymap.set("n", "<LeftRelease>", '"+y')   -- Paste with middle mouse click
vim.keymap.set("v", "<C-x>", '"+d')       -- cut
vim.keymap.set("n", "<C-v>", '"+p')       -- paste from system clipboard
vim.keymap.set("i", "<C-v>", '<ESC>"+pa') -- paste from system clipboard

-- Ctrl + ud 只移动10行，默认移动半屏
vim.keymap.set("n", "<C-u>", "5k")
vim.keymap.set("n", "<C-d>", "5j")

-- 单行或多行移动
-- M 表示 Alt 键。Up，Down 是方向键上下
-- map("v", "<M-Down>", ":m '>+1<CR>gv=gv")
-- map("v", "<M-Up>", ":m '<-2<CR>gv=gv")
vim.keymap.set("v", "<A-Down>", ":m '>+1<CR>gv=gv", { desc = "Line move down" })
vim.keymap.set("v", "<A-Up>", ":m '<-2<CR>gv=gv", { desc = "Line move up" })

-- 光标回到行首
vim.keymap.set({ "n", "x", "o" }, "H", "^", { desc = "Start of line" })
-- 光标回到行未
vim.keymap.set({ "n", "x", "o" }, "L", "$", { desc = " End of line" })

vim.keymap.set({ "n", "x" }, "<S-q>", "<Cmd>:q<CR>")
vim.keymap.set({ "n", "x" }, "Q", "<Cmd>:qa<CR>")

-- ctrl + o 与 ctrl + i  来回跳

-- 关闭 buffer
vim.keymap.set(
  "n",
  "<leader>bd",
  "<cmd>bd<CR>",
  { desc = "delete current buffer" }
)

-- 在所有缓冲区(buffers)之间循环
vim.keymap.set(
  "n",
  "<PageUp>",
  "<Cmd>bprevious<CR>",
  { desc = "buffer previous" }
)
vim.keymap.set("n", "<PageDown>", "<Cmd>bnext<CR>", { desc = "buffer next" })

vim.keymap.set("n", "<leader>cp", function()
  vim.notify(vim.api.nvim_buf_get_name(0))
end, { desc = "Show Current buffer file path" })
-- Copy current buffer file path to clipboard
vim.keymap.set(
  "n",
  "<leader>cpr",
  '<cmd>let @+=expand("%")<cr>',
  {
    desc = "[C]opy buffer [R]elative file [P]ath",
    noremap = true,
    silent = true,
  }
)
vim.keymap.set(
  "n",
  "<leader>cpa",
  '<cmd>let @+=expand("%:p")<cr>',
  {
    desc = "[C]opy buffer [A]bsolute file [P]ath",
    noremap = true,
    silent = true,
  }
)
vim.keymap.set(
  "n",
  "<leader>cpf",
  '<cmd>let @+=expand("%:t")<cr>',
  { desc = "[C]opy buffer [F]ile name [P]ath", noremap = true, silent = true }
)
vim.keymap.set(
  "n",
  "<leader>cpd",
  '<cmd>let @+=expand("%:p:h")<cr>',
  { desc = "[C]opy buffer [D]ir name [P]ath", noremap = true, silent = true }
)

-- Copy filepath to the clipboard
vim.keymap.set("n", "<leader>fa", function()
  local filePath = vim.fn.expand "%:~"                 -- Gets the file path relative to the home directory
  vim.fn.setreg("+", filePath)                         -- Copy the file path to the clipboard register
  print("File path copied to clipboard: " .. filePath) -- Optional: print message to confirm
end, { desc = "Copy file path to clipboard" })

-- Toggle LSP diagnostics visibility
local isLspDiagnosticsVisible = true
vim.keymap.set("n", "<leader>lx", function()
  isLspDiagnosticsVisible = not isLspDiagnosticsVisible
  vim.diagnostic.config {
    virtual_text = isLspDiagnosticsVisible,
    underline = isLspDiagnosticsVisible,
  }
end, { desc = "Toggle LSP diagnostics" })
